package org.apache.flink.runtime.metrics.groups;

import static org.apache.flink.util.Preconditions.checkNotNull;

import java.util.Map;

import javax.annotation.Nullable;

import org.apache.flink.annotation.Internal;
import org.apache.flink.metrics.CharacterFilter;
import org.apache.flink.runtime.metrics.MetricRegistry;
import org.apache.flink.runtime.metrics.dump.QueryScopeInfo;
import org.apache.flink.runtime.metrics.scope.ScopeFormat;
import org.apache.flink.runtime.resourceconsumer.ConsumeAttemptID;

/**
 * Special {@link org.apache.flink.metrics.MetricGroup} representing a resource slot.
 *
 */
@Internal
public class ResourceSlotMetricGroup extends ComponentMetricGroup<ResourceProviderConsumerMetricGroup> {

    static final int METRICS_OPERATOR_NAME_MAX_LENGTH = 80;
    
    /**
     * The execution Id uniquely identifying the executed task represented by this metrics group.
     */
    private final ConsumeAttemptID consumingId;
    private final String consumerName;
    private final int attemptNumber; // 资源slot申请次数

    // ------------------------------------------------------------------------
    ResourceSlotMetricGroup(
            MetricRegistry registry,
            ResourceProviderConsumerMetricGroup parent,
            ConsumeAttemptID executionId,
            String taskName) {
        super(
                registry,
                registry.getScopeFormats()
                        .getTaskFormat()
                        .formatScope(
                                checkNotNull(parent),
                                checkNotNull(executionId),
                                taskName,
                                checkNotNull(executionId).getAttemptNumber()),
                parent);

        this.consumingId = checkNotNull(executionId);
        this.consumerName = checkNotNull(taskName);
        this.attemptNumber = executionId.getAttemptNumber();

    }

    // ------------------------------------------------------------------------
    //  properties
    // ------------------------------------------------------------------------

    public final ResourceProviderConsumerMetricGroup parent() {
        return parent;
    }

    public ConsumeAttemptID executionId() {
        return consumingId;
    }

    @Nullable
    public String taskName() {
        return consumerName;
    }

    public int attemptNumber() {
        return attemptNumber;
    }

    @Override
    protected QueryScopeInfo.ResourceSlotQueryScopeInfo createQueryServiceMetricInfo(
            CharacterFilter filter) {
        return new QueryScopeInfo.ResourceSlotQueryScopeInfo(
                this.parent.consumerId.toString(),
                String.valueOf(consumingId.getConsumeItemId()),
                this.attemptNumber);
    }

    @Override
    public void close() {
        super.close();

        parent.removeTaskMetricGroup(consumingId);
    }

    // ------------------------------------------------------------------------
    //  Component Metric Group Specifics
    // ------------------------------------------------------------------------

    @Override
    protected void putVariables(Map<String, String> variables) {
        variables.put(ScopeFormat.SCOPE_TASK_NAME, consumerName);
        variables.put(ScopeFormat.SCOPE_TASK_ATTEMPT_ID, consumingId.toString());
        variables.put(ScopeFormat.SCOPE_TASK_ATTEMPT_NUM, String.valueOf(attemptNumber));
    }

    @Override
    protected String getGroupName(CharacterFilter filter) {
        return "task";
    }

	@Override
	protected Iterable<? extends ComponentMetricGroup> subComponents() {
		return null;
	}
}
